package com.i8office.android.zhihuixinwu.module.presenter;

import android.text.TextUtils;

import com.cyan.tools.callback.SimpleCallback;
import com.i8office.android.zhihuixinwu.bean.Code;
import com.i8office.android.zhihuixinwu.module.contract.CalendarContract;
import com.i8office.android.zhihuixinwu.module.model.CalendarModel;

import java.util.HashMap;
import java.util.Map;

public class AddEventPresenter extends BasePresenterImpl<CalendarContract.AddEventView> implements CalendarContract.AddEventPresenter {
    private CalendarContract.Model model;

    public AddEventPresenter(CalendarContract.AddEventView view) {
        super(view);
        model = new CalendarModel();
    }

    @Override
    public void addEvent(Map<String, String> params) {
        addDisposable(model.addEvent(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data.isSucceed()) {
                    mView.addSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, params));
    }

    @Override
    public void updateEvent(Map<String, String> params) {
        addDisposable(model.updateEvent(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data.isSucceed()) {
                    mView.addSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, params));
    }
    @Override
    public void updateOtherEvent(Map<String, String> params) {
        addDisposable(model.updateOtherEvent(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data.isSucceed()) {
                    mView.addSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, params));
    }

    @Override
    public void removeEvent(String taskID, String leaderId) {
        Map<String, String> params = new HashMap<>();
        params.put("idList", taskID);
        if (!TextUtils.isEmpty(leaderId)) {
            params.put("leaderId", leaderId);
        }
        addDisposable(model.removeEvent(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data.isSucceed()) {
                    mView.deleteSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, params));
    }
}
